package ynu.edu.item.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import ynu.edu.api.dto.OrderDetailDTO;
import ynu.edu.common.domain.stats.CategoryStat;
import ynu.edu.item.domain.po.Item;

import java.util.List;


public interface ItemMapper extends BaseMapper<Item> {

    @Update("UPDATE item SET stock = stock - #{num} WHERE id = #{itemId}")
    void updateStock(OrderDetailDTO orderDetail);

    @Update("UPDATE item SET stock = stock + #{num} WHERE id = #{itemId}")
    void restoreStock(OrderDetailDTO orderDetail);

    /**
     * 查询商品分类统计
     * @return
     */
    @Select("SELECT category, COUNT(*) AS count FROM item GROUP BY category")
    @Results({
            @Result(property = "category", column = "category"),
            @Result(property = "count", column = "count")
    })
    List<CategoryStat> selectCategoryStats();


    Long selectTotalCount();
}
